<?php
namespace app\common\controller;

use EasyWeChat\Foundation\Application;
use think\Controller;
use think\exception\HttpResponseException;
use think\Response;

class Base extends Controller
{
    protected function s($data){
        $result = [
            'error_code' => 0,
            'result'  => $data,
        ];
        $response = Response::create($result, 'json')->header([]);
        throw new HttpResponseException($response);
    }
    protected function e($message,$error_code=1){
        $result = [
            'error_code' => $error_code,
            'message'  => $message,
        ];
        $response = Response::create($result, 'json')->header([]);
        throw new HttpResponseException($response);
    }



    protected function appinit(){
        $mid=input("m");
        if(empty($mid)){
            $this->e("没有请求来源");
        }

        $this->manager=$manager=model("manager")->get($mid);
        if($manager==null){
            $this->e("管理账户不存在");
        }

        $wxoption = [
            'debug' => true,
            'app_id' => $manager->appId,
            'secret' => $manager->appSecret,
            'token' => $manager->Token,
            'log' => [
                'level' => 'debug',
                'file' => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ], 'oauth' => [
                'scopes' => ['snsapi_userinfo'],
                'callback' => "/wx/Wx/login_cb?mid=$mid",
            ],
            'payment' => [
                'merchant_id'        =>$manager->MCHID,
                'key'                => $manager->KEY,
            ],
        ];
        $this->app= new Application($wxoption);
    }


}
